package com.ctms.system.controller;

import com.ctms.common.utils.*;
import com.ctms.system.domain.ClassStudentDO;
import com.ctms.system.domain.UserDO;
import com.ctms.system.domain.UserRoleDO;
import com.ctms.system.service.StudentService;
import com.ctms.system.service.TeacherService;
import com.ctms.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RequestMapping("/personalCenter")
@Controller
public class PersonalCenterController {
    @Autowired
    StudentService studentService;
    @Autowired
    TeacherService teacherService;
    @Autowired
    UserService userService;

    @GetMapping("")
    public String student(Model model){
        UserDO user = ShiroUtils.getUser();
        int user_id = Integer.parseInt(Long.toString(user.getUserId()));
        int identity = user.getIdentity();

        if (identity == 2){
            UserDO userDO = studentService.selectById(user_id);
            Integer id = studentService.getClassIdByName(userDO.getStudentClass());
            userDO.setStudentClass(String.valueOf(id));
            model.addAttribute("student",userDO);
            return "sys/personalCenter/editStudent";
        }else if (identity == 1){
            UserDO userDO = teacherService.selectById(user_id);
            //避免出现矛盾
            if (userDO.getTeacherFrom()==0){
                userDO.setTeacherCompany("");
                teacherService.updateCompany(userDO);
            }else if (userDO.getTeacherFrom()==1){
                userDO.setTeacherCollege("");
                teacherService.updateCollege(userDO);
            }else {
                userDO.setTeacherCompany("");
                userDO.setTeacherCollege("");
                teacherService.updateCompany(userDO);
                teacherService.updateCollege(userDO);
            }
            model.addAttribute("teacher",userDO);
            return "sys/personalCenter/editTeacher";
        }else if (identity == 0){
            UserDO userDO = userService.selectUserById(user_id);
            model.addAttribute("admin",userDO);
            return "sys/personalCenter/editAdmin";
        }else {
            return "404";
        }

    }

    @PostMapping("/updateStudent")
    @ResponseBody
    R updateStudent(UserDO userDO){
        int user_id = Integer.parseInt(Long.toString(userDO.getUserId()));
        int class_id = Integer.parseInt(userDO.getStudentClass());
        String name = studentService.getClassNameById(class_id);
        userDO.setStudentClass(name);
        Date date=new Date();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        String time=simpleDateFormat.format(date);
        userDO.setGmtModified(time);
        if (studentService.updateStudent(userDO)>0){
            ClassStudentDO classStudentDO = new ClassStudentDO();
            classStudentDO.setUserId(user_id);
            classStudentDO.setClassId(class_id);
            studentService.updateClassStudent(classStudentDO);
            return R.ok();
        }else {
            return R.error();
        }
    }

    @PostMapping("/updateTeacher")
    @ResponseBody
    R updateTeacher(UserDO userDO){
        Date date=new Date();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        String time=simpleDateFormat.format(date);
        userDO.setGmtModified(time);
        if (teacherService.updateTeacher(userDO)>0){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @PostMapping("/updateAdmin")
    @ResponseBody
    R updateAdmin(UserDO userDO){
        Date date=new Date();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        String time=simpleDateFormat.format(date);
        userDO.setGmtModified(time);
        if (userService.updateAdmin(userDO)>0){
            return R.ok();
        }else {
            return R.error();
        }
    }
}
